home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.direct.ca!usenet
- From: qjackson@direct.ca
- Newsgroups: comp.lang.c++
- Subject: Re: Where do I put inline functions?
- Date: Sat, 02 Mar 1996 17:29:10 GMT
- Organization: Parsepolis Software
- Message-ID: <4ha0bb$297@aphex.direct.ca>
- References: <4h7sj0$10a@mother.usf.edu>
- Reply-To: qjackson@direct.ca
- NNTP-Posting-Host: 204.174.251.87
- X-Newsreader: Forte Free Agent 1.0.82
-
- millert@grad.csee.usf.edu (Timothy Miller) wrote:
-
- >Should I put inline functions in .cpp files like normal functions or in
- >.hpp files like #defines would be in C?
-
- "Should" is a dangerous word when it comes to programming.... ;-)
-
- A tolerable place to put them is in the .hpp file with the class
- definition, since they will be needed by the compiler for any file
- that includes the .hpp. (A bad place to put them would be the .cpp,
- since they are compiled only once and then are no longer visible to
- the compiler -- how can the compiler include them inline?)
-
- Some people put them in a file with an extension like .ipp and include
- that in the .hpp like this:
-
- #ifndef FOO_H
- #define FOO_H
-
- class foo
- {
-
- // ...
-
- };
-
- #include "foo.ipp"
-
- #endif
-
- Since inline functions are short and sweet, and not too hard to
- follow, a the "tolerable" approach yields:
-
- #ifndef FOO_H
- #define FOO_H
-
- class foo
- {
-
- void bar (void);
-
- // ...
-
- };
-
- inline
- void foo::bar (void)
- {
-
- // do some bar
-
- }
-
- #endif
-
- One of the beauties of the above -- if bar ever goes "outline", cut
- and paste to the .cpp is all that's required.
-
- The "worst" approach looks something like this:
-
- #ifndef FOO_H
- #define FOO_H
-
- class foo
- {
-
- void bar (void)
- {
-
- // Do some bar
-
- }
-
- // ...
-
- };
-
- #endif
-
- Except for the very simplest functions, such as formal virtual dtors,
- the above clogs up the class definition and requires a bit of work to
- extract a memfunc should it ever evolve and require outlining.
-
-
- And, of course, your mileage may vary.
-
-
- Cheers,
-
-
-
-
-
-
-
-
-
-
- --
- |
- Parsepolis Software | Quinn Tyler Jackson
- "ParseCity" | (aka 'Jamshid')
- >--------------------------| qjackson@direct.ca
- |---------------------->
-
-